<?php
//adding $points points for $id_ user
function add_user_points($id_='', $points=0) {
	global $conf;
	
	if(!$id_ && $points) exit;
	
	if($points) {
		$db=new dbquery;
		$db->query("UPDATE $conf[prefix_users]users SET points=points+".$points." WHERE id=".$id_) or $db->err(__FILE__, __LINE__);
		$db->clear_cache('user_by_id_'.$id_, 'users');
	}
}
//

//delete $points points for $id_ user
function delete_user_points($id_='', $points=0) {
	global $conf;
	
	if(!$id_ && $points) exit;
	
	if($points) {
		$db=new dbquery;
		$db->query("UPDATE $conf[prefix_users]users SET points=points-".$points." WHERE id=".$id_) or $db->err(__FILE__, __LINE__);
		$db->clear_cache('user_by_id_'.$id_, 'users');
	}
}
//

//functions return options for author to forms
function get_redactor_options($selected=NULL) {
	global $conf;
	
	if(!$selected) $selected=0;
	
	$db=new dbquery;
	$db->query("SELECT * FROM $conf[prefix_users]users WHERE gid IN (".implode(", ", get_redactor_gids()).") or id=".$selected." ORDER by gid ASC") or $db->err(__FILE__, __LINE__);
	
	$select_author=NULL;
	while($u=$db->fetch_object()) {
		if($u->id!=$selected)
			$select_author.='<option value="'.$u->id.'">'. $u->login .' ['.$u->name.' '.$u->surname.']</option>';
		else
			$select_author.='<option value="'.$u->id.'" selected>'. $u->login .' ['.$u->name.' '.$u->surname.']</option>';
	}
	
	return $select_author;
}
//
	
//return name and email and login of author, from id - array: [0] name [1] email [2] login
function get_user_by_id($id_)
{
	global $conf;

	SQLvalidate($id_);

	if(!$id_)
	$id_=0;

	$db = new dbquery;
	$db->cache('user_by_id_'.$id_, 'users');
	$db->clear_fields(array('pass', 'buddies'));
	$db->query("SELECT * FROM $conf[prefix_users]users WHERE id=$id_") or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object(true);

	return $d;
}
//

//function returns link to profile with overlib 
function get_user_overlib_by_id($id_)
{
	global $conf, $lang;

	SQLvalidate($id_);

	if(!$GLOBALS['get_user_overlib_by_id_'.$id_]){
		$db = new dbquery;
		$db->cache('user_by_id_'.$id_, 'users');
		$db->clear_fields(array('pass', 'buddies'));
		$db->query("SELECT * FROM $conf[prefix_users]users WHERE id=$id_") or $db->err(__FILE__, __LINE__);
		$d=$db->fetch_object(true);

		if(ereg("^http://", $d->avatar))
		$avatar='<br /><img src="'.$d->avatar.'" alt="" />';
		elseif($d->avatar && $d->avatar!='none')
		$avatar='<br /><img src="'.$conf['avatar_upload'].$d->avatar.'" alt="" />';

		$rank_=get_rank($d->gid, $d->points);

		$rank = $rank_['name'];
		$rank_ = $rank_['path'];

		if($rank_)
		$rank_image='<img src="'.$rank_.'" alt="'.$rank.'" />';

		$g = get_group_info_by_gid($d->gid);
		$gname = $g->name;
		$gcolor = $g->color;

		$overLIB='<b>'.$d->login.'</b><br /><small>'.$rank.'<br />'.$rank_image.'</small>'.$avatar.'<br /><br /><small>'.$lang['group'].' <b>'.$gname.'</b><br  />';
		if (is_module_installed('forum')) $overLIB.=$lang['forum_posts'].' <b>'.$d->posts.'</b><br />';
		$overLIB.=$lang['location'].' <b>'.read_text_rest($d->location).'</b></small>';
		$overLIB=overlib_text($overLIB);
		$overLIB="<a href=\"index.php?module=users&amp;user=$d->id\" onmouseover=\"return overlib('$overLIB', WRAP);\" onmouseout=\"return nd();\" style=\"color: $gcolor\" target=\"_parent\">$g->prefix$d->login</a>";

		if($_SESSION['id']==$id_)
		$GLOBALS['get_user_overlib_by_id_'.$id_]=$overLIB;

		return $overLIB;
	}
	else
	return $GLOBALS['get_user_overlib_by_id_'.$id_];
}
//

//funkcja zwraca link do strony autora...  bez overlibu
function get_user_link_by_id($id_, $class=NULL)
{
	global $conf, $lang;

	SQLvalidate($id_);

	if(!$GLOBALS['get_user_link_by_id_'.$id_]){
		$db = new dbquery;
		$db->cache('user_by_id_'.$id_, 'users');
    $db->clear_fields(array('pass', 'buddies'));
		$db->query("SELECT * FROM $conf[prefix_users]users WHERE id=$id_") or $db->err(__FILE__, __LINE__);
		$d=$db->fetch_object(true);

		$g = get_group_info_by_gid($d->gid);
		$overLIB="<a href=\"index.php?module=users&amp;user=$d->id\" target=\"_parent\"><span class=\"$class\" tyle=\"color: $g->color\">$g->prefix$d->login</span></a>";

		if($_SESSION['id']==$id_)
		$GLOBALS['get_user_link_by_id_'.$id_]=$overLIB;

		return $overLIB;
	}
	else
	return $GLOBALS['get_user_link_by_id_'.$id_];
}
//

//decoding img_password
function img_pwd_validate($pwd)
{
	$nr = floor(time() / 1800);
	for($i = 0; $i < strlen($pwd); $i++)
	if($i % 2 == 0)
	$a .= $pwd[$i];
	else
	$b .= $pwd[$i];
	$a = base_convert(strrev($a), 36, 10);
	$b = base_convert($b, 36, 10);
	if($a + $b > $nr || $a + $b < $nr - 1)
	return false;
	else
	return true;
}
//

//generating unique pass
function passgen() { 
	$nr = floor(time() / 1800); 
	srand(array_sum(explode(" ", microtime())) * 100000); 
	$a = rand(0, min(getrandmax(), $nr)); 
	$b = $nr - $a; 
	$a = base_convert($a, 10, 36); 
	$b = base_convert($b, 10, 36); 
	$a = strrev(str_pad($a, max(strlen($a), strlen($b)), 0, STR_PAD_LEFT)); 
	$b = str_pad($b, max(strlen($a), strlen($b)), 0, STR_PAD_LEFT); 
	for($i = 0; $i < strlen($a); $i++) $pass .= $a[$i].$b[$i]; 
	
	return $pass; 
}
//

//generating image with passgen() key
function pass_image() {
	global $conf;
	
	if($conf['code_img_bg_'.$_SESSION['theme']])
	{
	  $bg['red'] = $conf['code_img_bg_'.$_SESSION['theme']]['red'];
	  $bg['green'] = $conf['code_img_bg_'.$_SESSION['theme']]['green'];
	  $bg['blue'] = $conf['code_img_bg_'.$_SESSION['theme']]['blue'];
	}
	else
	{
	  $bg['red'] = $conf['code_img_bg_default']['red'];
	  $bg['green'] = $conf['code_img_bg_default']['green'];
	  $bg['blue'] = $conf['code_img_bg_default']['blue'];
	}
	  
	if($conf['code_img_tx_'.$_SESSION['theme']])
	{
	  $tx['red'] = $conf['code_img_tx_'.$_SESSION['theme']]['red'];
	  $tx['green'] = $conf['code_img_tx_'.$_SESSION['theme']]['green'];
	  $tx['blue'] = $conf['code_img_tx_'.$_SESSION['theme']]['blue'];
	}
	else
	{
	  $tx['red'] = $conf['code_img_tx_default']['red'];
	  $tx['green'] = $conf['code_img_tx_default']['green'];
	  $tx['blue'] = $conf['code_img_tx_default']['blue'];
	}

	header("Content-type: image/jpeg"); 
	$img = imagecreate(80, 20); 
	$background = imagecolorallocate($img, $bg['red'], $bg['green'], $bg['blue']); 
	$textcolor = imagecolorallocate($img, $tx['red'], $tx['green'], $tx['blue']); 
	imagestring($img, 5, 3, 3, passgen(), $textcolor); 
	imagejpeg($img); 
	imagedestroy($img);
}

function lost_password()
{
	global $conf;

	$db=new dbquery;

	if($_POST['login'] or $_POST['email'])
	{

		if($_POST['login'])
		$sql="login='$_POST[login]'";
		if($_POST['email'])
		$sql="mail='$_POST[email]'";

		$db->query("SELECT * FROM $conf[prefix_users]users WHERE $sql") or $db->err(__FILE__, __LINE__);

		if($db->num_rows()==0)
		{
			redirect('index.php?module=error&action=users&error=error1');
			exit;
		}

		$d=$db->fetch_object();

		$code=md5($d->pass);
		$id_=$d->id;

		$link=$conf['adres'].'index.php?module=users&function= users_lost_password_send&id='.$id_.'&code='.$code;

		$title=string_template($GLOBALS['lang']['users']['lost_pass_title'], array('adress'=>$conf['adress']));
		$message=string_template($GLOBALS['lang']['users']['lost_pass_msg'], array('link'=>$link));

		if(!@mail($d->mail, $title, $message, "From: $conf[mailing_from]")) {
			redirect('index.php?module=error&action=users&error=mailing_error');
			exit;
		}
		else {
			redirect('index.php?module=info&info=lost_pass_info1');
			exit;
		}
	}

	redirect('index.php');
	exit;
}

?>
